package com.example.myapplication.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.utils.DBHelper;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
public class RegisterActivity extends AppCompatActivity {
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().hide();//隐藏标题栏

        // 初始化 SQLite 数据库
        DBHelper dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();

        EditText accountEdit = findViewById(R.id.editText4);
        EditText passwordEdit = findViewById(R.id.editText5);
        EditText confirmEdit = findViewById(R.id.editText6);
        Button registerButton = findViewById(R.id.button3);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountEdit.getText().toString();
                String password = passwordEdit.getText().toString();
                String confirm = confirmEdit.getText().toString();

                //不能为空
                if(account.isEmpty() || password.isEmpty() || confirm.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "信息不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 检查密码是否一致
                if (!password.equals(confirm)) {
                    Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 检查账号是否已存在
                if (isAccountExists(account)) {
                    Toast.makeText(RegisterActivity.this, "账号已存在", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 添加用户信息到数据库
                addUserToDatabase(account, password);

                // 注册成功后可以返回登录界面，并传递注册成功的信息
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                intent.putExtra("registrationSuccess", true);
                startActivity(intent);
                finish(); // 结束当前注册界面，返回登录界面

            }
        });
    }

    private boolean isAccountExists(String account) {
        Cursor cursor = db.rawQuery("SELECT * FROM user WHERE account=?", new String[]{account});
        int count = cursor.getCount();
        cursor.close();
        return count > 0;
    }

    private void addUserToDatabase(String account, String password) {
        ContentValues values = new ContentValues();
        values.put("account", account);
        values.put("password", password);
        db.insert("user", null, values);
    }
}
